#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Contains the interface for the GUI.
"""

import wx
import os.path

class GuiIo(wx.Frame):
    def __init__(self, parent, title, size):
        super(GuiIo, self).__init__(
                parent, 
                title=title,
                size=size)
        self._init_menubar()
        self._init_window()
        self._bind_events()
        self.title = title
               
        self.Show()
        return
    
    def _init_menubar(self):
        """Creates the menubar for the program upon creation."""
        self.menubar = wx.MenuBar()
        
        self.fileMenu = wx.Menu()
        
        _ = self.fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
        self.Bind(wx.EVT_MENU, self._evt_exit, _)
        
        self.menubar.Append(self.fileMenu, '&File')
        
        self.SetMenuBar(self.menubar)
        return
    
    def _init_window(self):
        """Creates the window elements"""
        self.panel = wx.Panel(self)
        self.box = wx.BoxSizer(wx.HORIZONTAL)
        
        self.input = _InputPanel(self.panel)
        
        self.box.Add(self.input, proportion=1, flag=wx.ALL|wx.EXPAND, border=5)
        self.panel.SetSizer(self.box)
        return
        
    def _bind_events(self):
        pass
    
    def _get_title(self):
        return self._title
    def _set_title(self, value):
        self._title = value
        self.SetTitle(value)
        return
    title = property(_get_title, _set_title)
    
    def _get_size(self):
        return self._size
    def _set_size(self, value):
        self._size = tuple(value)
        self.SetSize(self._size)
        return
    size = property(_get_size, _set_size)
    
    def _evt_exit(self, event):
        self.Close()
        raise SystemExit
        return
        

class _InputPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.sizer = wx.FlexGridSizer(rows=1, cols=3, vgap=5, hgap=5)
        
        self.label = wx.StaticText(self, wx.ID_ANY, "File:", style=wx.ALIGN_LEFT)
        self.input_box = wx.TextCtrl(self)
        self.load = wx.Button(self, wx.ID_ANY, "Load Vocab File")

        self.Bind(wx.EVT_BUTTON, self.load_file, self.load)

        self.sizer.AddGrowableCol(1, proportion=1)
        
        self.sizer.AddMany([
            (self.label), (self.input_box, 1, wx.EXPAND), (self.load)])
        self.SetSizer(self.sizer)
        return

    def load_file(self, event):
        self.dirname = ''
        dialog = wx.FileDialog(
            self,
            "chose a file",
            self.dirname,
            "",
            "*.txt",
            wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            path = os.path.join(dialog.GetDirectory(), dialog.GetFilename())
            self.input_box.SetValue(path)
        dialog.Destroy()
        return
        
        
def main():
    app = wx.App()
    GuiIo(None, title="test", size=(600, 400))
    app.MainLoop()
    return

if __name__ == '__main__':
    main()
